package com.yanqu.road.web.controller.usercenter.newadmin;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.admin.AdminResult;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.utils.config.Config;
import com.yanqu.road.utils.http.HttpHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;

/**
 * 70.数据录入上传数数（新通用接口）
 */
@RestController
@RequestMapping("/usercenter/admin")
public class CenterAdminDataThinkController {

    /**
     *  70.1 新增/修改
     *  全部转发到区服处理
     */
    @ResponseBody
    @RequestMapping(value = "counting/uploadEvent", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public String event(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        int channelId = jsonObject.getIntValue("channelId");
        ServerInfo serverInfo = null;
        if(Config.isDebug()){
            serverInfo = ServerListMgr.getServerInfo(99938028L);
        }else {
            List<ServerInfo> serverList = ServerListMgr.getChannelServerList(channelId);
            if(!serverList.isEmpty()){
                int i = new Random().nextInt(serverList.size());
                serverInfo = serverList.get(i);
            }
        }
        if(serverInfo == null){
            return AdminResult.failMsg("渠道下没有区服").toJsonString();
        }

        String response = HttpHelper.doPostJson(serverInfo.getWebServerUrl() + "/game/admin/counting/uploadEvent", jsonObject.toJSONString(), "UTF-8");
        if(!StringUtils.isNullOrEmpty(response)){
            return response;
        }

        return AdminResult.failMsg("").toJsonString();
    }

}
